package tema10.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import tema10.persistence.ContactoDAO;
import tema10.persistence.ContactoPersistFactory;
import tema10.model.Contacto;

@WebServlet(name="DeleteServlet", urlPatterns={"/tema10/deleteServlet"})
public class DeleteServlet extends BasicUtilitiesServlet {
   
    @Override
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        ContactoDAO persistenceManager = ContactoPersistFactory.getContactoDAO(persistenceMechanism);
        String nombre = request.getParameter("nombre");
        String apellidos = request.getParameter("apellidos");
        String id = persistenceManager.searchIDContacto(nombre, apellidos);
        if (id != null) {
            Contacto contacto = persistenceManager.readContacto(id);
            if (persistenceManager.deleteContacto(id)) {
                request.setAttribute("contacto", contacto);
                request.setAttribute("operacion", "BORRADO");
                request.setAttribute("persistencia", persistenceMechanism);
                gotoURL(successForm, request, response);
            } else {
                gotoURL(errorForm, request, response);
            }
        } else {
            request.setAttribute("operacion", "CONTACTO A BORRAR NO ENCONTRADO");
            gotoURL(errorForm, request, response);
        }
    }
}
